package com.hdw.med.controller;

import com.hdw.common.base.BaseException;
import com.hdw.common.base.BaseResult;
import com.hdw.common.constant.CommonResultConstant;
import com.hdw.common.vo.LayMsg;
import com.hdw.common.vo.PaginationVO;
import com.hdw.med.api.BaseSdItemService;
import com.hdw.med.bean.model.BaseSdItem;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.management.remote.rmi._RMIConnection_Stub;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * baseSdItem控制器
 * Created by hozeData on 2021/05/21.
 */
@RestController
@RequestMapping("/med/baseSdItem/")
@Api( tags="基础字典项")
public class BaseSdItemController {

    @Autowired
    BaseSdItemService baseSdItemService;

    @ApiOperation(value="根据字典主键查询")
    @RequestMapping(value="findBySd", method=RequestMethod.GET)
    public LayMsg findBySd(Long idSd) {
        List<BaseSdItem> pageVO = null;
        if(idSd!=0) {
            BaseSdItem item = new BaseSdItem();
            item.setIdSd(idSd);
            pageVO = baseSdItemService.findByModel(item);
        }
        return LayMsg.success().addData(pageVO);
    }

    @ApiOperation(value="根据字典主键查询")
    @RequestMapping(value="findByCdSd", method=RequestMethod.GET)
    public LayMsg findBySd(String cdSd) {
        List<BaseSdItem> pageVO = null;
        if(StringUtils.isNotEmpty(cdSd)) {
            BaseSdItem item = new BaseSdItem();
            item.setCdSd(cdSd);
            pageVO = baseSdItemService.findByModel(item);
        }
        return LayMsg.success().addData(pageVO);
    }

    @ApiOperation(value="保存")
    @RequestMapping(value="save", method=RequestMethod.POST)
    public BaseResult insert(@RequestBody BaseSdItem baseSdItem, HttpServletRequest request)  {
        int num;
        if(baseSdItem.getIdSdItem()==null) {
            num = baseSdItemService.insertSelective(baseSdItem);
        } else {
            num = baseSdItemService.updateByIdSelective(baseSdItem);
        }
        return new BaseResult(CommonResultConstant.SUCCESS, num);
    }

    @ApiOperation(value="详情")
    @ApiImplicitParam(name="idSdItem", value="主键", paramType="query", required=true, dataType="Long")
    @RequestMapping(value="getById", method=RequestMethod.GET)
    public BaseResult getById(Long idSdItem, HttpServletRequest request) throws BaseException {
        BaseSdItem baseSdItem = baseSdItemService.getById(idSdItem);
        return new BaseResult(CommonResultConstant.SUCCESS, baseSdItem);
    }

    @ApiOperation(value="根据主键删除")
    @ApiImplicitParam(name="idSdItem", value="主键", paramType="query", required=true, dataType="Long")
    @RequestMapping(value="delete", method=RequestMethod.GET)
    public BaseResult delete(Long idSdItem, HttpServletRequest request) throws BaseException {
        int num = baseSdItemService.deleteById(idSdItem);
        return new BaseResult(CommonResultConstant.SUCCESS, num);
    }



}
